package com.chy.blog_web.modular.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_web.core.util.SpaceUserUtil;
import com.chy.blog_web.modular.web.entity.BoInfoSet;
import com.chy.blog_web.modular.web.entity.BoSpaceUser;
import com.chy.blog_web.modular.web.service.IBoInfoSetService;
import com.chy.blog_web.modular.web.service.IBoSpaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：我的空间登录 controller
 * 作者：chenhuiyan
 * 时间：2020/04/11
 */
@Controller
@RequestMapping(value = "/blogSpaceLogin")
public class mySpaceLoginController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/myspace/";

    @Autowired
    private IBoSpaceUserService spaceUserService;

    @Autowired
    private IBoInfoSetService infoSetService;


    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping(value = "")
    public String index(Model model){
        //判断空间是否开放
        Map<String, Object> infoSet = infoSetService.getList().get(0);
        if(Integer.parseInt(infoSet.get("isOpen") + "") == 0){
            model.addAttribute("spaceOpen", "未开放");
        }else{
            model.addAttribute("spaceOpen", "已开放");
        }
        return PREFIX + "login";
    }

    /**
     * 空间用户登录验证
     * @param account
     * @param password
     * @return
     */
    @RequestMapping(value = "/loginVerify")
    @ResponseBody
    public Map<String, Object> loginVerify(String account, String password, HttpSession session, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{

            //根据空间用户名查找用户
            BoSpaceUser spaceUser = spaceUserService.getOne(new QueryWrapper<BoSpaceUser>().eq("account", account));

            //判断空间是否开放
            Map<String, Object> infoSet = infoSetService.getList().get(0);
            if(Integer.parseInt(infoSet.get("isOpen") + "") == 0){
                if(spaceUser.getType() == 2){
                    res.put("state", "500");
                    res.put("message", "空间暂时不对外开放");
                    return res;
                }
            }

            if(ObjectUtil.isNotNull(spaceUser)){
                //帐号状态验证
                if(0 == spaceUser.getStatus()){
                    //密码验证(长度大于25视为已是MD5加密的密码)
                    if(password.length() > 25){
                        if(password.equals(spaceUser.getPassword())){

                            spaceUser.setPassword(""); //密码设置为空

                            //保存登录用户到session
                            request.getSession().setAttribute("spaceUser", spaceUser);
                            request.getSession().setMaxInactiveInterval(12 * 60 * 60); //session失效时间
//                            SpaceUserUtil.getSession();
//                            SpaceUserUtil.putUserIntoSession(spaceUser);

                            res.put("state", "200");
                            res.put("message", "登录成功");
                            return res;
                        }else{
                            res.put("state", "500");
                            res.put("message", "帐号或密码错误");
                            return res;
                        }
                    }else{
                        String passwordToMd5 = SecureUtil.md5(password);
                        if(spaceUser.getPassword().equals(passwordToMd5)){

                            spaceUser.setPassword(""); //密码设置为空

                            //保存登录用户到session
                            request.getSession().setAttribute("spaceUser", spaceUser);
                            request.getSession().setMaxInactiveInterval(5 * 24 * 60 * 60); //session失效时间
//                            SpaceUserUtil.getSession();
//                            SpaceUserUtil.putUserIntoSession(spaceUser);

                            res.put("state", "200");
                            res.put("message", "登录成功");
                            return res;
                        }else{
                            res.put("state", "500");
                            res.put("message", "帐号或密码错误");
                            return res;
                        }
                    }

                }else if(1 == spaceUser.getStatus()){
                    res.put("state", "500");
                    res.put("message", "该帐号已被冻结");
                    return res;
                }else{
                    res.put("state", "500");
                    res.put("message", "该帐号已被封停");
                    return res;
                }
            }else{
                res.put("state", "500");
                res.put("message", "帐号或密码错误");
                return res;
            }

        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，登录失败！");
        }
        return res;
    }

}
